// router.post("/addwallcomment", reqComments_handler.addReqComments);
// router.get("/getwallcomment", reqComments_handler.getReqComments);

const db = require("../db/index");

/**
 * 发布问题评论
 * {request_id message }
 * @param {request_id message } req
 * @param {*} res 0
 */
exports.addReqComments = (req, res) => {
  const sql = `insert into request_comment set ? `;
  const sql2 = `update request set comment_num = comment_num+1 where id = ?`;
  const commentData = {
    ...req.body,
    user_id: req.user.id,
    moment: new Date(),
  };
  db.query(sql, commentData, (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.cc("发布评论失败");
    db.query(sql2, req.body.request_id, (err, result) => {
      res.cc("发布评论成功", 0);
    });
  });
};

/**
 * 获取每个卡片中的评论
 *params
 * @param {request_id} req
 * @param {request_comment.message,request_comment.like_num,request_comment.moment,users.username } res
 */
exports.getReqComments = (req, res) => {
  const sql = `select request_comment.id,request_comment.message,request_comment.like_num,request_comment.moment,users.username from request_comment inner join users on request_comment.user_id = users.id where request_comment.request_id = ? and request_comment.is_delete != 1 order by request_comment.moment desc`;
  db.query(sql, req.query.request_id, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      message: "获取成功",
      data: results,
      status: 0,
    });
  });
};

/**
 *
 * @param {comment_id } req
 * @param {*} res
 */

// 点赞
exports.updateReqLike = (req, res) => {
  const sql = `update request_comment set like_num  = like_num + 1 where id = ? `;
 

  db.query(sql, req.body.comment_id, (err, results) => {
    if (err) return err;
    const sql2 = `insert into request_like set ?`;

    const likeInfo = {
      ...req.body,
      moment: new Date(),
      // 作者id
      user_id: req.user.id,
    };
    db.query(sql2, likeInfo, (err, result) => {
      if (err) return err;
      res.cc("点赞成功", 0);
    });
  });
};

// 查询点赞的comment_id 红心显示
exports.findUserReqCommentLike = (req, res) => {
  // const sql = `select * form request_like where user_id = ? `;
  const sql = `select * from request_like where user_id = ?`;
 
  db.query(sql, req.user.id, (err, results) => {
    if (err) return err;
    res.send({
      message: "获取成功",
      status: 0,
      data: results,
    });
  });
};

/**
 *
 * @param {获取用户评论 id request_id message moment req_moment } req
 * @param {*} res
 */
exports.getMyComReq = (req, res) => {
  // const sql = `select * form request_like where user_id = ? `;
  const sql = `select request_comment.* ,users.username as users_username,request.title,request.moment as req_moment from request_comment,users,request where request_comment.user_id = ? and request_comment.request_id = request.id and users.id = ? and request_comment.is_delete !=1`;

  db.query(sql, [req.user.id, req.user.id], (err, results) => {
 
    if (err) return err;
    res.send({
      message: "获取成功",
      status: 0,
      data: results,
    });
  });
};

/**
 * 删除我的评论
 * @param {com_id} req
 * @param {*} res
 */
exports.delteMyCom = (req, res) => {
  const sql = `update request_comment set is_delete = 1 where id =  ?`;
  db.query(sql, req.query.com_id, (err, results) => {
    if (err) return res.cc(err);
    res.cc("删除成功", 0);
  });
};
